package de.ars.client.items;

import java.util.Vector;

import de.ars.client.datahandling.IPersonListener;
import de.ars.client.items.elements.AbstractItem;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;

public class EditItem extends AbstractSlideItem
{    
	String oldValue = "";
	
    public EditItem(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
    {
    	super(_width,_height,_parent,_stringPainter);

    	this.animator = new EditItemAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getEditClosePos());
        this.animator.createNewAnimationPoint(this.layout.getEditCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getEditCloseTextPos());
        this.animator.setLayout(this.layout);
        
        this.entryItems = new Vector();
    }
    
    public AbstractItem getEditingItem()
    {
    	return ((AbstractItem)this.entryItems.elementAt(0));
    }
    
    private void saveChanges()
    {    	
    	this.parent.newPersonAction(IPersonListener.ACTION_EDITPERSON, null);
    }
    
    public boolean keyPressed(int _key)
	{
    	if (isOpen())
    	{
	    	switch(_key)
	    	{
	    		case -6:
	    			((AbstractItem)this.entryItems.elementAt(0)).setValue(oldValue);
	    			close();
	    			return true;
	    		case -7:
	    			saveChanges();
	    			close();
	    			return true;
	    		default:
	    			((AbstractItem)this.entryItems.elementAt(0)).keyPressed(_key);	
	    			return true;
	    	}
    	}
    	return false;
	}
    
    public void open()
    {
    	super.open();
    	this.entryItems = new Vector();
    	this.entryItems.addElement(this.parent.getItem().getSelectedItem());
    	oldValue = ((AbstractItem)this.entryItems.elementAt(0)).getValue();
    }
}